﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ue.Framework.Web.WebControls;
using Ue.Infrastructure.Web.UI;
using Ue.Qcdz.Domain.Membership;

namespace Ue.Qcdz.UI.Controls.Membership
{
    public class GetPassWord : SkinnedControl
    {
        private string skinFileName = "~/Themes/zh-cn/Skins/GetPassword.ascx";
        private TextBox tbxPassword;
        private TextBox tbxPasswordAg;
        private Button btnButton;
        protected override string GetSkinFileName()
        {
            return skinFileName;
        }

        protected override void InitializeSkin(Control skin)
        {
            string uid = Page.Request["Id"];
            string key = Page.Request["Key"];
            string mail = this.Page.Request["Mail"];
            if (!string.IsNullOrEmpty(uid) && !string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(mail))
            {
                if (key.Equals(Ue.Framework.Security.DEncryptUtils.MD5EnCode(uid)))
                {
                    tbxPassword = skin.FindControl<TextBox>("tbxPassword");
                    tbxPasswordAg = skin.FindControl<TextBox>("tbxPasswordAg");
                    btnButton = skin.FindControl<Button>("btnButton");
                    btnButton.Click += new EventHandler(btnButton_Click);
                }
                else
                {
                    MessageWindow.RenderMessage("请勿修改url信息","Default.aspx");
                }
            }
            else
            {
                MessageWindow.RenderMessage("请勿修改url信息", "Default.aspx");
            }

        }
        void btnButton_Click(object sender, EventArgs e)
        {
            string uid = Page.Request["Id"];
            string mail = this.Page.Request["Mail"];
            Guid userId = new Guid(uid);
            IUserRepository repository =
                Ue.Infrastructure.RepositoryFramework.RepositoryFactory.GetRepository<IUserRepository, User>();
            User user = repository.FindBy(userId);
            if (Ue.Framework.Security.DEncryptUtils.MD5EnCode(user.UserName).Equals(mail))
            {
                string strPassword = tbxPassword.GetValue<string>();
                if (user.ChangePassword(user.Password, strPassword))
                {
                    MessageWindow.RenderMessage("修改密码成功,请自行登录", "login.aspx");
                }
                else
                {
                    MessageWindow.RenderMessage("密码修改失败");
                }
            }
            else
            {
                MessageWindow.RenderMessage("请勿修改url信息", "Default.aspx");
            }
        }
    }
}
